package com.peanut.baby.mvp.msg;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.AppMsg;
import com.peanut.baby.model.BaseNotification;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.bbs.detail.BBSMomentDetailActivity;
import com.peanut.baby.mvp.follow.FollowFansActivity;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.baby.mvp.web.WebViewActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class MsgActivity extends BaseMvpActivity implements TitleLayout.OnTitleClickListener,
    PullRecyclerView.OnRecyclerRefreshListener,MsgContract.View,AppMsgRecyclerAdapter.OnItemClickListener{

    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.msg_recycler)
    PullRecyclerView msgRecycler;

    private AppMsgRecyclerAdapter   adapter;
    private List<AppMsg>            msg;

    private int pageNo = 1;
    private int pageSize = 10;

    private MsgPresenter presenter;
    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, MsgActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ButterKnife.bind(this);
        presenter = new MsgPresenter(this,this);
        initViews();
    }

    private void initViews() {
        title.setOnTitleClickedListener(this);
        title.setTitle("消息");


        msgRecycler.enableLoadMore(true);
        msgRecycler.enablePullRefresh(true);
        msgRecycler.setOnRecyclerRefreshListener(this);
        msgRecycler.setLayoutManager(new XLinearLayoutManager(this));

        msg = new ArrayList<>();
        adapter = new AppMsgRecyclerAdapter(this,msg);
        adapter.setOnItemClickListener(this);
        msgRecycler.setAdapter(adapter);

        msgRecycler.postRefreshing();

    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getMsgList(pageNo,pageSize);
    }

    @Override
    public void onLoadMore() {
        pageNo ++;
        presenter.getMsgList(pageNo,pageSize);
    }

    @Override
    public void onMsgGet(List<AppMsg> _msgs) {
        stopRefreshOrLoading();
        if(pageNo == 1){
            msg.clear();
        }
        if(_msgs != null && !_msgs.isEmpty()){
            msg.addAll(_msgs);
        }
        adapter.notifyDataSetChanged();
        msgRecycler.enableLoadMore(_msgs != null && _msgs.size() >= pageSize);
    }

    @Override
    public void onGetMsgFailed(String reason) {
        stopRefreshOrLoading();
        msgRecycler.enableLoadMore(false);
        showToast("加载失败 "+reason);
    }

    private void stopRefreshOrLoading(){
        msgRecycler.stopRefresh();
        msgRecycler.stopLoadMore();
    }

    @Override
    public void onItemClick(AppMsg msg) {
        presenter.setMsgReaded(msg);
        msg.readFlag = 1;
        if(InitManager.getInstance().unreadCount>0) {
            InitManager.getInstance().unreadCount = InitManager.getInstance().unreadCount - 1;
        }
        adapter.notifyDataSetChanged();
        if(!StringUtil.isNullOrEmpty(msg.url)){
            WebViewActivity.start(MsgActivity.this,msg.url,msg.title,"");
        }else{
            if(!StringUtil.isNullOrEmpty(msg.json)){
                Gson gson = new Gson();
                BaseNotification noti = gson.fromJson(msg.json,BaseNotification.class);
                if(noti.type == BaseNotification.TYPE_LIVE || noti.type == BaseNotification.TYPE_NEW_LIVE){
                    LiveDetailActivity.start(MsgActivity.this,Integer.parseInt(noti.bizId));
                }else if(noti.type == BaseNotification.TYPE_NEW_FOLLOWER){
                    FollowFansActivity.start(MsgActivity.this,false, InitManager.getInstance().getUserId());
                }else if(noti.type == BaseNotification.TYPE_QA_REPLY){
                    QADetailActivity.start(MsgActivity.this,Integer.parseInt(noti.bizId));
                }else if(noti.type == BaseNotification.TYPE_BBS_REPLY){
                    BBSMomentDetailActivity.start(MsgActivity.this,noti.bizId);
                }
            }
        }
    }
}
